﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Charities.aspx.cs" Inherits="DonationTracker.Charities1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" >
        function text_change(t) {
            var val = t.value;
            var l = val.length;
            if (val == "") { l = 0; }
            var btn = document.getElementById('MainContent_grd1_btnSave');
            if (l > 0) {
                btn.disabled = false;
            } else {
                btn.disabled = true;
            }
        }

        window.onload = function () {
            var d = document.getElementById('gridContainer');
            var g = document.getElementById('MainContent_grd1');
            d.style.width = g.offsetWidth + "px";
            g.align = "center";
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Charities Page</h2>
    <br /><br />
    <asp:SqlDataSource ID="donationTracker_dataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DonationConn %>" 
        SelectCommand="SELECT * FROM [charity]" 
        ConflictDetection="CompareAllValues" 
        DeleteCommand="DELETE FROM [charity] WHERE [charityID] = @original_charityID AND [name] = @original_name AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL))" 
        InsertCommand="INSERT INTO [charity] ([name], [description]) VALUES (@name, @description)" 
        OldValuesParameterFormatString="original_{0}" 
        UpdateCommand="UPDATE [charity] SET [name] = @name, [description] = @description WHERE [charityID] = @original_charityID AND [name] = @original_name AND (([description] = @original_description) OR ([description] IS NULL AND @original_description IS NULL))">
        <DeleteParameters>
            <asp:Parameter Name="original_charityID" Type="Int32" />
            <asp:Parameter Name="original_name" Type="String" />
            <asp:Parameter Name="original_description" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="description" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="description" Type="String" />
            <asp:Parameter Name="original_charityID" Type="Int32" />
            <asp:Parameter Name="original_name" Type="String" />
            <asp:Parameter Name="original_description" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div id="gridContainer" style="width: 800px; max-width: 960px; margin: 0px auto;" >
        <asp:GridView ID="grd1" runat="server" style="max-height: 400px; width: 800px; margin:0px auto;" 
            AutoGenerateColumns="False" DataKeyNames="charityID" 
            DataSourceID="donationTracker_dataSource" AllowPaging="True" 
            ShowHeaderWhenEmpty="True" onrowcommand="grd1_RowCommand" 
            onrowupdating="grd1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="charityID" InsertVisible="False" 
                    SortExpression="charityID">
                    <FooterTemplate>
                        <asp:Button ID="btnSave" runat="server" CommandName="Insert" Enabled="False" 
                            Text="Save" /><br />
                        <asp:Button ID="btnCancel" runat="server" CausesValidation="False" 
                            Text="Cancel" onclick="btnCancel_Click" />
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("charityID") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name" SortExpression="name">
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewCharityName" runat="server" Columns="25" MaxLength="50" 
                            TextMode="MultiLine" onkeyup="text_change(this)" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvNewCharityName" runat="server" 
                            ControlToValidate="txtNewCharityName" Display="Dynamic" 
                            ErrorMessage="Please enter an charity name."></asp:RequiredFieldValidator>
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditCharityName" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="description" SortExpression="description">
                    <FooterTemplate>
                        <asp:TextBox ID="txtNewDescription" runat="server" Columns="60" 
                            MaxLength="150" TextMode="MultiLine" ></asp:TextBox>
                    </FooterTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditDescription" runat="server" Text='<%# Bind("description") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("description") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>
        <asp:Button ID="btnAddCharity" runat="server" Text="Add Charity" 
            onclick="btnAddCharity_Click" />
    </div>
</asp:Content>